﻿using RimWorld;
using Verse;
using System.Collections.Generic;
using Verse.AI.Group;
using System;

namespace RimZombieGirl
{
    public class IncidentWorker_DaoPriestsPassBy : IncidentWorker_GiveDaoNeutralBase
    {
        private static readonly IntRange PriestCountRange = new IntRange(2, 5);

        protected override bool ExecuteNeutralEvent(IncidentParms parms, Map map, Faction daoFaction)
        {
            if (!RCellFinder.TryFindRandomPawnEntryCell(out IntVec3 entryCell, map, CellFinder.EdgeRoadChance_Animal))
                return false;

            List<Pawn> pawns = new List<Pawn>();
            for (int i = 0; i < PriestCountRange.RandomInRange; i++)
            {
                PawnKindDef priestKind = DefDatabase<PawnKindDef>.GetNamedSilentFail("RI_DaoPawn_Priest");                    

                Pawn pawn = PawnGenerator.GeneratePawn(priestKind, daoFaction);
                GenSpawn.Spawn(pawn, CellFinder.RandomClosewalkCellNear(entryCell, map, 5), map);
                pawns.Add(pawn);
            }

            if (pawns.Count == 0)
                return false;

            IntVec3 chillSpot = CellFinder.RandomClosewalkCellNear(entryCell, map, 10);
            LordJob lordJob = new LordJob_VisitColony(daoFaction, chillSpot, null);
            LordMaker.MakeNewLord(daoFaction, lordJob, map, pawns);

            TaggedString letterText = "RI_DaoPriestsPassBy.letterText".Translate();
            TaggedString letterLabel = "RI_DaoPriestsPassBy.letterLabel".Translate();
            SendStandardLetter(letterLabel, letterText, LetterDefOf.PositiveEvent, parms, pawns, Array.Empty<NamedArgument>());

            return true;
        }
    }
}
